init('Describable');
(function(){
	var commandService = require('CommandService'),
	       StringUtils = require('StringUtils'),
	       Collections = require('Collections'),
	  propertyAccessor = require('ObjectUtils').propertyAccessor,    
  AutoCompleteResponse = require('AutoCompleteResponse').ctor;

    var format     = StringUtils.format,
        formatEach = StringUtils.formatEach;
    
    var combine    = Collections.combine,
        List       = Collections.List;
	
	commandService.commands['describe'] = {
		_findMatches : function(value, onDone){
			var session = this.session;
			var items = [];
			List.addAll(items, session.getUser().carryables );
			List.addAll(items, session.getUser().wearables );
			List.addAll(items, session.getRoom().items);
			
			var rooms = [];
			rooms.push(session.getRoom());
			List.addAll(rooms, session.getRoom().exits);
			
			var mobs = [];
			List.addAll(mobs, session.getRoom().mobiles);
			
			var response = new AutoCompleteResponse(
				value
				,[{
					values:rooms
					, _toString : propertyAccessor('data.name')
					, toHTML : '<span>${data.name}</span>&nbsp;'
				},{
					values:mobs
					, _toString : propertyAccessor('data.username')
					, toHTML : '<span>${data.username}</span>&nbsp;'
				},{
					values:items
					, _toString : propertyAccessor('data.name')
					, toHTML : '<span>${data.name}</span>&nbsp;'
				}]
			);
			console.log(response);
			
			onDone(response);
		},
		onAutoComplete : function(value){
			this._findMatches(value, this.onResponse);
		},
		onExecute : function(str){
			var finish = this.onResponse;
			var onDescribe = function(value){
				//override
				if( value.data.describe ){
					value.describe = eval('('+value.data.describe+')');
				}
				if( value.describe )
					finish( value.describe() );
				else
					finish( value );
			}
			
			if(!str){
				onDescribe(this.session.room);
			}else{
				this._findMatches(str, function(autoCompleteResponse){
					var matches = autoCompleteResponse.values;
					if( matches && matches.length ){
						onDescribe(matches[0]);
					}else{
						finish( format("'${value}' not found.", {value:str}) );
					}
				});
			}
		}	
	};
})();